﻿using AutoMapper;
using Common.ORM.Repository;
using DotNetCore.CAP;
using FreeRedis;
using Microsoft.AspNetCore.Mvc;
using sixgod.Common;
using sixgod.Common.Helper;
using sixgod.Common.Model;
using sixgod.Dtos.User;
using sixgod.Dtos.User.Search;
using sixgod.Model.Attribute;
using SqlSugar;

namespace sixgod.Service.User.Implementation;

[Di(typeof(IUserService))]
public class UserService:Repository<Model.User.User>,IUserService
{
    private ISqlSugarClient _db;
    private RedisClient _redisClient;
    private IMapper _mapper;
    /// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
    public UserService(ISqlSugarClient db, RedisClient redisClient, IMapper mapper) : base(redisClient,db)
    {
        this._db = db;
        _redisClient = redisClient;
        _mapper = mapper;
    }

    /// <summary>
    /// 获取所有用户
    /// </summary>
    /// <returns></returns>
    public async Task<List<UserSelectDto>> GetAllList()
    {
        var list = await GetListByRedis(CacheKeyConsts.USER_LIST);
        var result = _mapper.Map<List<UserSelectDto>>(list);
        return result;
    }

    /// <summary>
    /// 获取分页数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<PageResponseResult<UserDto>> GetPageList(UserQueryModel data)
    {
        var totalNumber = new RefAsync<int>();
        var list = await Context.Queryable<Model.User.User>()
            .Includes(r=>r.Department)
            .Includes(r=>r.Job)
            .Where(r=>r.IsDelete == 0).OrderByDescending(r=>r.UpdateTime)
            .ToPageListAsync(data.PageNum,data.PageSize,totalNumber);
        PageResponseResult<UserDto> result = new PageResponseResult<UserDto>()
        {
            Count = totalNumber,
            Data = _mapper.Map<List<UserDto>>(list)
        };
        return result;
    }

    public async Task<bool> InsertAsync(Model.User.User user, ICapPublisher capBus)
    {
        var verify = user.Verify();
        // if (!verify.Item1)
        // {
        //     
        // }

        user.Create(UserContext.Current.UserToken.UserId);
        await Context.Insertable(user).ExecuteCommandAsync();
        await capBus.PublishAsync("syncAddUser",user);
        return true;
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="user"></param>
    /// <param name="capBus"></param>
    /// <returns></returns>
    public async Task UpdateAsync(Model.User.User user, ICapPublisher capBus)
    {
        user.Verify();
        user.Update(UserContext.Current.UserToken.UserId);
        await Context.Updateable(user).IgnoreColumns(r=> new {r.IsDelete,r.Password}).ExecuteCommandAsync();
        await capBus.PublishAsync("syncUpdateUser", await GetByIdAsync(user.Id));
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <param name="capBus"></param>
    public async Task DeleteAsync(string id, ICapPublisher capBus)
    {
        if (id == "-1")
        {
            throw new Exception("管理员不能被删除");
        }
        await DeleteByIdAsync(id);
        await capBus.PublishAsync("syncDeleteUser",id);

    }
}